/**
 * Created by suntao on 17-7-21.
 */
var errFormatMsg = "<errmsg class='c-red f-12 ml-20'><i class='Hui-iconfont'>&#xe60b;</i><label class='f-12 ml-5'>格式不正确</label></errmsg>";
var errNullMsg = "<errmsg class='c-red f-12 ml-20'><i class='Hui-iconfont'>&#xe60b;</i><label class='f-12 ml-5'>不能为空</label></errmsg>";
var errUniquenessMsg = "<errmsg class='c-red f-12 ml-20'><i class='Hui-iconfont'>&#xe60b;</i><label class='f-12 ml-5'>名称已存在</label></errmsg>";
var correctMsg = "<errmsg class='c-success f-16 ml-20'><i class='Hui-iconfont'>&#xe6a7;</i></errmsg>";

function isEmpty(str) {
    if (str === undefined) {
        return true;
    }
    if (str === null) {
        return true;
    }
    var temp = str + "";
    if (temp.replace(/(^\s*)|(\s*$)/g, "").length == 0) {
        return true;
    } else {
        return false;
    }

}
function  getChineseLen(value) {
    value=$.trim(value);
    var strLen = value.length;
    var newStr = value.replace(/[A-Za-z0-9]/g, "");
    return newStr.length+strLen;
}

function formReset(id) {
    document.getElementById(id).reset();
}
function add0(m) {
    return m < 10 ? '0' + m : m
}
function getYesterday() {
    var day = new Date();
    day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
    return day.getFullYear() +  "-" + add0(day.getMonth() + 1) + "-" + add0(day.getDate())
}
function getLastYear() {
    var day = new Date();
    day.setTime(day.getTime() - 365*24 * 60 * 60 * 1000);
    return day.getFullYear() +  "-" + add0(day.getMonth() + 1) + "-" + add0(day.getDate())
}
function getLastMonth() {
    var day = new Date();
    day.setTime(day.getTime() - 30*24 * 60 * 60 * 1000);
    return day.getFullYear() +  "-" + add0(day.getMonth() + 1) + "-" + add0(day.getDate())
}
function getToday() {
    var day = new Date();
    return day.getFullYear() +  "-" + add0(day.getMonth() + 1) + "-" + add0(day.getDate())
}

//按照选择开始日期来判断结束日期是否符合要求,返回值是否符合要求和最大日期。
function isCleanEndDate(strartDate,endDate) {
    var todaystr=getToday();
    var todayTime=new Date(todaystr.replace(/-/g,"/")).getTime();
    var endDateTime=new Date(endDate.replace(/-/g,"/")).getTime();
    var bdDate=new Date(strartDate.replace(/-/g,"/"));
    var maxDate=31536000000+bdDate.getTime();
    var maxDateStr;
    if(maxDate>todayTime){
        maxDate=todayTime;
        maxDateStr=todaystr;
    }else{
        maxDateStr=(bdDate.getFullYear()+1)+'-'+add0(bdDate.getMonth() + 1)+'-'+add0(bdDate.getDate());
    }
    return {"ispass":(endDateTime > bdDate.getTime() && endDateTime < maxDate),"maxDay":maxDateStr};
}
//获取结束日期可设置最大天数值
function getMaxDay(beforeDay) {
        //注意beforeDay为WdatePicker获取的日期对象 格式为{y: 2014, M: 11, d: 1, H: 16, m: 52, …}
        var todaystr=getToday();
        var todayTime=new Date(todaystr.replace(/-/g,"/")).getTime();
        var bdDate=new Date(beforeDay.y+"/"+beforeDay.M+"/"+beforeDay.d);
        if(31536000000+bdDate.getTime()>todayTime){
            return todaystr;
        }else{
            return (bdDate.getFullYear()+1)+'-'+add0(bdDate.getMonth() + 1)+'-'+add0(bdDate.getDate());
        }
}

// yyyy-MM-dd HH:mm:ss
function dateObjToString(obj) {

    var ret = "";
    var year = "", month = "", date = "", hour = "", minute = "", second = "";
    if (obj !== null && obj !== undefined && obj.length !== 0) {
        if (obj.year) {
            year = obj.year + 1900;
            if (obj.month + 1 < 10) {
                month = "0" + (obj.month + 1);
            } else {
                month = obj.month + 1;
            }
            if (obj.date < 10) {
                date = "0" + obj.date;
            } else {
                date = obj.date;
            }
            if (obj.hours < 10) {
                hour = "0" + obj.hours;
            } else {
                hour = obj.hours;
            }
            if (obj.minutes < 10) {
                minute = "0" + obj.minutes;
            } else {
                minute = obj.minutes;
            }
            if (obj.seconds < 10) {
                second = "0" + obj.seconds;
            } else {
                second = obj.seconds;
            }
            ret = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
        }
    }

    return ret;
}
function dateFormat(data) {
    var ret = "";
    if (data !== undefined && data !== null) {
        var time = new Date(data);
        var y = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        ret = y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    }

    return ret;
}

// HH:mm:ss
function secondFormat(duration) {
    var ret = "";
    var time = parseInt(duration);

    var minute = "", second = "";
    if (time >= 0 && time < 60) {
        ret = "00:" + add0(time);
    } else if (time >= 60) {
        minute = parseInt(time / 60);
        ret = add0(minute);
        ret += ":";
        second = time % 60;
        ret += add0(second);
    } else {
        ret = "--:--";
    }
    return ret;
}

function regexCheckName(name) {
    var regex = /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
    if (regex.test(name)) {
        return true;
    } else {
        return false;
    }
}

function regexCheckName2(name) {
    var regex = /^(?!_)(?!.*?_$)[a-zA-Z0-9_]+$/;
    if (regex.test(name)) {
        return true;
    } else {
        return false;
    }
}

function checkNameCorrect($this, name, url, $btnConfirm) {
    $this.parent().find('errmsg').remove();
    if (isEmpty(name)) {
        $this.after(errNullMsg);
        // $btnConfirm.addClass("disabled");
        return;
    }
    if (!regexCheckName(name)) {
        $this.after(errFormatMsg);
        // $btnConfirm.addClass("disabled");
        return;
    }

    $.ajax({
        url: url,
        async: 'true',
        dataType: 'json',
        success: function (data) {
            if (data['errorCode'] === "0") {
                $this.after(correctMsg);
                // $btnConfirm.removeClass("disabled");
            } else {
                $this.after(errUniquenessMsg);
                // $btnConfirm.addClass("disabled");
            }
        }
    });

}

function myNotify(type, position, msg) {
    if(exitNotify()){
        return false;
    }
    var myType = 'info';
    var myPosition = 'center bottom';
    if (type != "") {
        myType = type;
    }
    if (position != "") {
        myPosition = position;
    }
    return Lobibox.notify(myType, {
        size: 'mini',
        width:500,
        rounded: true,
        delay:2000,
        delayIndicator: false,
        sound: false,
        position: myPosition,
        msg: msg
    });
}

//判断页面中是否存在弹出框
function exitNotify() {
    return $(".lobibox-notify").length>0?true:false;
}

function isPhoneAvailable(str) {
    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
    return myreg.test(str);
}

function getNumPercent(num, total) {
    if (num == 0 || total == 0) {
        return "0.00%"
    }
    return (Math.round(num / total * 10000) / 100.00 + "%");// 小数点后两位百分比
}

function keep2DecimalDigits(num) {
    return (Math.round(num * 100) / 100.00);// 小数点后两位百分比
}

function keep2DecimalDigits2(num1, num2) {
	if (num1 == 0 || num2 == 0) {
	    return "0.00"
	}
	return (num1/num2).toFixed(2)
}


//验证括号匹配
function matchkuohao(str) {
    str=str.replace(/[^\(\)\{\}\[\]]/g,"");
    str=str.split("");
    for(var i=0;i<str.length;i++){
        if(i != 0 && ismatch(str[i],str[i-1])){
            str.splice(i-1,2);
            i-=2;
        }
    }
    return str.length==0?true:false;
}
function ismatch(b,a){
    return a=="{" && b=="}" ||a=="[" && b=="]" ||a=="(" && b==")";
}


//系统通用规则
function CheckName(a,b,c,d) {
    //参数a 为传过来的名字，参数b为是否允许中文,c是否允许括号,d中文是否算1字符
    a=$.trim(a);
    if(isEmpty(a)){
        return {"pass":false,msg:"名称不能为空"}
    }
    if(b){
        if(getChineseLen(a)>60&&!d){
            return {"pass":false,msg:"名称不能大于60(一个汉字等于2字符)"}
        }

		if(d&&a.length>60) {
			return {"pass":false,msg:"名称不能大于60"}
		}

        if(c){
            if(!(/^[\w\u4e00-\u9fa5()（）]+$/.test( a )&&/^[^_]/.test( a )&&/[^_]$/.test( a ))){
                return {"pass":false,msg:"名称有特殊字符，请输入字母数字下划线括号和汉字且不以下划线开头结尾"}
            }
        }else{
            if(!(/^[\w\u4e00-\u9fa5]+$/.test( a )&&/^[^_]/.test( a )&&/[^_]$/.test( a ))){
                return {"pass":false,msg:"名称有特殊字符，请输入字母数字下划线和汉字且不以下划线开头结尾"}
            }
        }

    }else{
        if(a.length>60){
            return {"pass":false,msg:"名称不能大于60"}
        }
        if(!/^\w+$/.test(a)){
            return {"pass":false,msg:"名称有特殊字符，请输入字母数字下划线"}
        }
    }
    return {"pass":true,msg:"通过"}
}
function CheckPassWd(a) {
    //a 为密码
    a=$.trim(a);
    if(isEmpty(a)){
        return {"pass":false,msg:"密码不能为空"}
    }
    if(a.length>16){
        return {"pass":false,msg:"密码不能大于16个字符"}
    }
    if(a.length< 6){
        return {"pass":false,msg:"密码不能小于6"}
    }
    if(!/^\w+$/.test(a)){
        return {"pass":false,msg:"密码中有特殊字符，请输入字母数字下划线"}
    }
    return {"pass":true,msg:"通过"}
}

//将毫秒转化为秒
function setValToInt(doms) {
    //dom为jquery对象
    doms.each(function (index,dom) {
        dom.value=parseInt(dom.value/1000);
    })
}
function getMyLen(value) {
    var strLen = value.length;
    var newStr = value.replace(/[A-Za-z0-9_]/g, "");
    return newStr.length+strLen;
}